Imports System.Runtime.Serialization
Imports System.Runtime.Versioning

'Namespace cv2.File
''' <summary>
''' Base Configuration Holder class. Holds all Infomation on a Bikirom file
''' </summary>
''' <remarks></remarks>
''' 
<Serializable()> Friend Class CarBase
    ' Implements ISerializable

    <NonSerialized()> Private m_config As CarConfig = Nothing
    Private m_bikConfig As BikSettings = Nothing
    Private m_configID As Integer = 0
    Private m_vars As BVars = Nothing

    Private m_dataStart As Integer = 0
    Private m_dataEnd As Integer = &HFFFF
    Private m_baseData() As Byte
    Private m_mapOne() As Byte = Nothing
    Private m_mapTwo() As Byte = Nothing
    Private m_mapThree() As Byte = Nothing

    <NonSerialized()> Friend Const EEPROM_SIZE As UInt16 = &HD00
    <NonSerialized()> Friend Const MAP_SIZE As UInt32 = &H10000
    <NonSerialized()> Friend Const MAP1_LOCATION As UInt32 = 0
    <NonSerialized()> Friend Const MAP2_LOCATION As UInt32 = &H10000
    <NonSerialized()> Friend Const MAP3_LOCATION As UInt32 = &H20000
    <NonSerialized()> Friend Const CFG_LOCATION As UInt32 = &H30000

    <OptionalField(VersionAdded:=2)> _
    Private m_consultDB As DataLogging.ConsultLogDB = Nothing

    <OnDeserializing()> _
    Private Sub setVersion2Added(ByVal sc As StreamingContext)
        Debug.Print("VERSION 2 ADDED")
        If IsNothing(m_consultDB) Then m_consultDB = New DataLogging.ConsultLogDB
    End Sub

    Sub New()
        m_vars = New BVars("Car Variables")
        m_dataStart = &H1000
        m_dataEnd = &HFFFF
        m_bikConfig = New BikSettings(m_vars)
        ReDim m_baseData(m_dataEnd - m_dataStart)
    End Sub
    Sub New(ByRef Config As CarConfig)
        m_vars = New BVars("Car Variables")
        m_bikConfig = New BikSettings(m_vars)
        m_dataStart = &H1000
        m_dataEnd = &HFFFF
        m_config = Config
        If Not IsNothing(m_config) Then
            Dim f_tmVar As BVar = m_config.getBikiVars.getByName("FLASHstart")
            If Not IsNothing(f_tmVar) Then m_dataStart = f_tmVar.Value
            f_tmVar = m_config.getBikiVars.getByName("FLASHend")
            If Not IsNothing(f_tmVar) Then m_dataEnd = f_tmVar.Value
        End If
        ReDim m_baseData(m_dataEnd - m_dataStart)
    End Sub
    Public Property DataStart() As Integer
        Get
            Return m_dataStart
        End Get
        Set(ByVal value As Integer)
            m_dataStart = value
        End Set
    End Property
    Public Property DataEnd() As Integer
        Get
            Return m_dataEnd
        End Get
        Set(ByVal value As Integer)
            m_dataEnd = value
        End Set
    End Property

    Friend Function getConfig() As CarConfig
        Return m_config
    End Function
    Public Property ConfigID() As Integer
        Get
            Return m_configID
        End Get
        Set(ByVal value As Integer)
            m_configID = value
        End Set
    End Property
    Friend Sub setConfig(ByRef Config As CarConfig)
        m_config = Config
        If Not IsNothing(m_config) Then
            Dim f_tmVar As BVar = m_config.getBikiVars.getByName("FLASHstart")
            If Not IsNothing(f_tmVar) Then m_dataStart = f_tmVar.Value
            f_tmVar = m_config.getBikiVars.getByName("FLASHend")
            If Not IsNothing(f_tmVar) Then m_dataEnd = f_tmVar.Value
        End If
        If UBound(m_baseData) <> m_dataEnd - m_dataStart Then
            ReDim m_baseData(m_dataEnd - m_dataStart)
        End If
    End Sub

    Friend Sub setVars(ByRef variables As BVars)
        m_vars = variables
    End Sub
    Friend Function getVars() As BVars
        Return m_vars
    End Function

    ''' <summary>
    ''' Returns the Datalogging database for this Bikirom File
    ''' </summary>
    ''' <returns>DataLogging Database</returns>
    ''' <remarks></remarks>
    Friend Function getDataLogDB() As DataLogging.ConsultLogDB
        If IsNothing(m_consultDB) Then m_consultDB = New DataLogging.ConsultLogDB
        If m_consultDB.Count = 0 Then m_consultDB = New DataLogging.ConsultLogDB
        Return m_consultDB
    End Function

    Friend Function getMap(ByVal Number As Byte) As Byte()
        Dim f_return() As Byte = Nothing
        If Number >= 0 And Number <= 2 Then
            ReDim f_return(m_dataEnd)
            For i As UInt32 = 0 To UBound(f_return)
                f_return(i) = &HFF
            Next
            Select Case Number
                Case 0
                    Dim f_offset As Integer = UBound(m_mapOne) - (m_dataEnd - m_dataStart)
                    array_copy(m_mapOne, f_offset, f_return, m_dataStart, m_dataEnd - m_dataStart + 1)
                Case 1
                    Dim f_offset As Integer = UBound(m_mapTwo) - (m_dataEnd - m_dataStart)
                    array_copy(m_mapTwo, f_offset, f_return, m_dataStart, m_dataEnd - m_dataStart + 1)
                Case 2
                    Dim f_offset As Integer = UBound(m_mapThree) - (m_dataEnd - m_dataStart)
                    array_copy(m_mapThree, f_offset, f_return, m_dataStart, m_dataEnd - m_dataStart + 1)
            End Select
        End If
        Return f_return
    End Function
    Friend Function getMapRaw(ByVal Number As Byte) As Byte()
        Select Case Number
            Case 0
                Return m_mapOne
            Case 1
                Return m_mapTwo
            Case 2
                Return m_mapThree
            Case Else
                Return Nothing
        End Select
    End Function
    Friend Sub setMap(ByRef Array() As Byte, ByVal Number As Byte)
        If Number >= 0 And Number <= 2 Then
            Dim f_data(m_dataEnd - m_dataStart) As Byte
            array_copy(Array, m_dataStart, f_data, 0, m_dataEnd - m_dataStart + 1)
            Select Case Number
                Case 0
                    m_mapOne = f_data
                Case 1
                    m_mapTwo = f_data
                Case 2
                    m_mapThree = f_data
            End Select
        End If
    End Sub
    Friend Function mapTransfer(ByRef FromArray As Byte(), ByRef ToArray As Byte()) As String
        If Not IsNothing(FromArray) And Not ToArray Is Nothing Then
            Dim f_return As String = ""
            If Not IsNothing(m_config) Then
                If Not IsNothing(m_config.getTables) Then
                    For Each tab As Table In m_config.getTables.getTablesAsArray
                        If tab.Importable Then
                            f_return &= tab.Name & Chr(10) & Chr(13)
                            tab.setRaw(ToArray, tab.getRaw(FromArray))
                            If Not tab.getWidthScale Is Nothing Then If tab.getWidthScale.Address >= 0 Then tab.getWidthScale.setRaw(ToArray, tab.getWidthScale.getRaw(FromArray, tab.Width))
                            If Not tab.getHeightScale Is Nothing Then If tab.getHeightScale.Address >= 0 Then tab.getHeightScale.setRaw(ToArray, tab.getHeightScale.getRaw(FromArray, tab.Width))
                        End If
                    Next
                End If
                If Not IsNothing(m_config.getGlobals) Then
                    For Each var_ As Var In m_config.getGlobals.getAsArray
                        If var_.Importable Then
                            f_return &= var_.Name & Chr(10) & Chr(13)
                            var_.SetValue(ToArray, var_.getValue(FromArray))
                        End If
                    Next
                End If
            End If
            Return f_return
        Else
            Return ""
        End If
    End Function
    Friend Sub mapTransferAll(ByRef fromarray As Byte(), ByRef toarray As Byte())
        If Not IsNothing(fromarray) And Not toarray Is Nothing Then
            If Not IsNothing(m_config) Then
                If Not IsNothing(m_config.getTables) Then
                    For Each tab As Table In m_config.getTables.getTablesAsArray
                        If Not IsNothing(tab) Then
                            If tab.Address <= UBound(toarray) - tab.Length And tab.Address >= 0 Then
                                Console.WriteLine("Table: " & tab.Description.Description)
                                tab.setRaw(toarray, tab.getRaw(fromarray))
                                If Not tab.getWidthScale Is Nothing Then If tab.getWidthScale.Address >= 0 Then tab.getWidthScale.setRaw(toarray, tab.getWidthScale.getRaw(fromarray, tab.Width))
                                If Not tab.getHeightScale Is Nothing Then If tab.getHeightScale.Address >= 0 Then tab.getHeightScale.setRaw(toarray, tab.getHeightScale.getRaw(fromarray, tab.Width))
                            End If
                        End If
                    Next
                End If
                If Not IsNothing(m_config.getGlobals) AndAlso m_config.getGlobals.count > 0 Then
                    For Each var_ As Var In m_config.getGlobals.getAsArray
                        If Not IsNothing(var_) Then
                            var_.SetValue(toarray, var_.getValue(fromarray))
                        End If
                    Next
                End If
            End If
        End If
    End Sub

    Friend Sub setForUpload()
        m_mapOne = Nothing
        m_mapTwo = Nothing
        m_mapThree = Nothing
        m_bikConfig = Nothing
        m_config = Nothing
        m_consultDB = Nothing
    End Sub
    Friend Function getBikSettings() As BikSettings
        ' If IsNothing(m_bikConfig) Then m_bikConfig = New BikSettings()
        Return m_bikConfig
    End Function
    Friend Sub setBikSettings(ByRef setting As BikSettings)
        m_bikConfig = setting
    End Sub

    Friend Sub baseImport(ByRef Array_() As Byte)
        If UBound(Array_) >= m_dataEnd - 1 Then
            For i As Integer = m_dataStart To m_dataEnd
                m_baseData(i - m_dataStart) = Array_(i)
            Next
        End If
    End Sub
    Friend Sub baseImport(ByVal fileName As String)
        Dim fnum As Integer = FreeFile()
        FileOpen(fnum, fileName, OpenMode.Binary)

        'If Not IsNothing(Me.getVars.getByName("flashstart")) Then m_dataStart = Me.getVars.getByName("flashstart").Value
        'If Not IsNothing(Me.getVars.getByName("flashend")) Then m_dataStart = Me.getVars.getByName("flashend").Value
        If LOF(fnum) <> 0 Then
            ReDim m_baseData(m_dataEnd - m_dataStart)
            Dim f_lenTo As Long = LOF(fnum)
            Dim f_offset As Long
            If LOF(fnum) = 65536 Then
                f_offset = 0
            ElseIf LOF(fnum) = 32768 And IsNothing(m_vars.getByName("27C256")) Then
                f_offset = 32768
            End If
            If f_lenTo - 1 > m_dataEnd Then
                f_lenTo = m_dataEnd + 1
            End If
            For i As Integer = m_dataStart - f_offset To CInt(f_lenTo - 1)
                FileGet(fnum, m_baseData(i - m_dataStart + f_offset), i + 1)
            Next
        End If
        FileClose(fnum)
    End Sub
    Friend Function baseExport() As Byte()
        Dim f_export(0 To m_dataEnd) As Byte
        For i As Integer = m_dataStart To m_dataEnd
            f_export(i) = m_baseData(i - m_dataStart)
        Next
        Return f_export
    End Function
    Friend Function baseExportRaw() As Byte()
        Return m_baseData
    End Function

    Friend Function TP_calc(ByRef Array_() As Byte, ByVal RPM As Integer, ByVal afm As Integer, ByVal rafm As Integer) As Double
        Dim returnByte As Double = 0
        Dim f_kmult As Double = 1
        Dim f_kVar As Var = Me.getConfig.getGlobals.getByName("ISF")
        Dim vq As Integer = VQ_Value(Array_, afm) + VQ_Value(Array_, rafm)
        If RPM > 0 And Not IsNothing(f_kVar) Then
            Dim f_mult As BVar = Me.m_vars.getByName("KMULT")
            If Not IsNothing(f_mult) Then f_kmult = f_mult.Value
            returnByte = (f_kVar.getValue(Array_) * f_kmult * (vq / 32768)) / (RPM / 12.5)
        Else
            returnByte = 0
        End If
        Return returnByte
    End Function
    Friend Function VQ_Value(ByRef Array_() As Byte, ByVal Mvolts As Integer) As Integer
        Dim f_return As Integer = 0
        Dim f_vq As Table = Me.getConfig.getTables.getTableByName("Sensor - VQ Table")
        If Not IsNothing(f_vq) And Mvolts <= 5120 And Mvolts >= 0 Then
            Dim f_step As Integer = 5120 \ (f_vq.Width * f_vq.Height - 1)
            Dim f_offset As Integer = Mvolts \ f_step
            'Debug.Print(f_offset)
            Dim f_split As Double = (Mvolts - (f_offset * f_step)) / f_step
            f_return = f_vq.getValue(Array_, f_offset)
            If f_offset - 1 < (f_vq.Width * f_vq.Height) Then
                f_return *= (1 - f_split)
                f_return += f_vq.getValue(Array_, f_offset + 1) * f_split
            End If
        End If
        Return f_return
    End Function
End Class
'End Namespace